package com.example.tys.daggermvp.network.okhttpclient;

import android.util.Log;

import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import okhttp3.OkHttpClient;

/**
 * @author tys
 * @date 2018/4/25
 */

public class SSLOkHttpClient extends BaseOkHttpClient {

    private static final String TAG = "SSLOkHttpClient";

    @Override
    protected OkHttpClient.Builder customize(OkHttpClient.Builder builder) {
        return builder.
                sslSocketFactory(TrustAll.SSLSocketFactory.createSSLSocketFactory()).
                hostnameVerifier(new TrustAll.TrustAllHostnameVerifier());
    }

    static class TrustAll {
        static class SSLSocketFactory {
            static javax.net.ssl.SSLSocketFactory createSSLSocketFactory() {
                javax.net.ssl.SSLSocketFactory sSLSocketFactory = null;
                try {
                    SSLContext sc = SSLContext.getInstance("TLS");
                    sc.init(null, new TrustManager[]{new TrustAllManager()}, new SecureRandom());
                    sSLSocketFactory = sc.getSocketFactory();
                } catch (Exception e) {
                    Log.e(TAG, e.toString());
                }
                return sSLSocketFactory;
            }
        }

        static class TrustAllManager implements X509TrustManager {
            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType)
                    throws CertificateException {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType)
                    throws CertificateException {
            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        }

        private static class TrustAllHostnameVerifier implements HostnameVerifier {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        }
    }

    static class TrustSpecified{
        static class SSLSocketFactory {
            static javax.net.ssl.SSLSocketFactory createSSLSocketFactory() {
                return null;
            }
        }

        static class TrustAllManager implements X509TrustManager{
            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

            }

            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {

            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        }

        private static class TrustAllHostnameVerifier implements HostnameVerifier {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        }
    }
}
